import { createRouter, createWebHistory } from "vue-router";
import layout from "../views/layout/layout.vue"
import functionModule from "./modules/functionModule" // 功能模块
import systemModule from "./modules/systemModule"; // 系统管理
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import WorkbenchLayout from "../views/layout/components/WorkbenchLayout.vue"
// 路由信息
const routes = [
    {
        path: "/",
        name: "Workbench",
        component: WorkbenchLayout,
        redirect: '/Workbench',
        children: [
            {
                path: "/Workbench",
                name: "Workbench",
                component: () => import('../views/workbench.vue'),
                meta: {
                    type: 'item',
                    label: 'tes11t',
                    icon: 'workbench'
                }
            },
        ]
    },
    // 系统管理
    {
        path: "/system",
        name: "System",
        component: layout,
        redirect: '/system',
        children: [
            systemModule,
            {
                path: "/system/Systemtest",
                name: "Systemtest",
                component: () => import('../views/test.vue'),
                meta: {
                    type: 'item',
                    label: '系统管理test',
                    icon: 'workbench'
                }
            },
        ]
    },
    {
        path: "/baseData",
        name: "BaseData",
        component: layout,
        redirect: '/baseData',
        children: [
            // functionModule,
            {
                path: "/baseData",
                name: "BaseDatatest",
                component: () => import('../views/test.vue'),
                meta: {
                    type: 'item',
                    label: '基础数据test',
                    icon: 'workbench'
                }
            },
        ]
    },
    {
        name: '登录',
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/index.vue'),
    },
];

// 导出路由
const router = createRouter({
    history: createWebHistory(),
    routes,
});
// 全局前置守卫 判断是否登录
router.beforeEach((to, from, next) => {
    NProgress.start();

    let token = localStorage.getItem('Token');
    if (to.name == 'Login' && token) {
        next({ name: 'Workbench' })
    }
    if (to.name !== 'Login' && !token) next({ name: 'Login' })
    else next()
});

router.afterEach(() => {
    NProgress.done();
});
export default router;
